package com.bolovan.infovaluta.reader;

import java.io.IOException;
import java.math.BigDecimal;

import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.bolovan.infovaluta.bean.ExchangeRate;
import com.bolovan.infovaluta.bean.ExchangeRateEntry;
import com.bolovan.infovaluta.bean.ExchangeRateSeller;
import com.bolovan.infovaluta.util.RateMaps;
import com.bolovan.infovaluta.util.RateUtil;

public class RaiffeisenReader extends ExchangeReader {

	public RaiffeisenReader(ExchangeRateSeller seller) {
		super(seller);
	}

	@Override
	public void buildRateMap(ExchangeRate map) throws IOException {
		fetchRaiffeisen(map);
	}

	private void fetchRaiffeisen(ExchangeRate map) throws IOException {
		setParsingDate(RateMaps.date);

		Document doc = RateUtil.getDocument(ExchangeRateSeller.RAIFFEISEN.getUrl());
		Elements elements = doc.select("div.rzbContentTextNormal").first().select("tr:not(.rzbTableHeader)");

		for (Element e : elements) {

			Elements tds = e.select("td");
			if (tds.size() == 7) {
				String currency = tds.get(1).text().trim();
				if (currency.equals("TRL")) {
					currency = "TRY";
				}
				BigDecimal bid = new BigDecimal(tds.get(3).text().trim());
				BigDecimal ask = new BigDecimal(tds.get(4).text().trim());
				map.addToMap(new ExchangeRateEntry(currency, bid, ask));
			}
		}
	}
}
